/*package com.sxc.oa.web.action;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.sxc.oa.utils.DButil.close;
import static com.sxc.oa.utils.DButil.getConnction;

public class MemberDelServlet extends HttpServlet {
    private static final Logger LOGGER = Logger.getLogger(MemberDelServlet.class.getName());

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String deptno = request.getParameter("deptno");
        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = getConnction();
            conn.setAutoCommit(false);

            String sql = "DELETE FROM sss WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1, Integer.parseInt(id));
            int count = ps.executeUpdate();

            conn.commit();

            if (count == 1) {
                // 删除成功，重定向到部门详情页面
                response.sendRedirect(request.getContextPath() + "/dept/detail?deptno=" + deptno);
            } else {
                // 删除失败
                request.setAttribute("errorMsg", "删除成员失败！");
                request.getRequestDispatcher("/error1.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            LOGGER.log(Level.SEVERE, "SQL Exception occurred", e);
            try {
                if (conn != null) {
                    conn.rollback();
                }
            } catch (SQLException ex) {
                LOGGER.log(Level.SEVERE, "Failed to rollback transaction", ex);
            }
            throw new ServletException("Failed to delete member", e);
        } finally {
            try {
                close(conn, ps, null);
            } catch (SQLException e) {
                LOGGER.log(Level.SEVERE, "Failed to close resources", e);
            }
        }
    }
}*/
package com.sxc.oa.web.action;

import com.sxc.oa.utils.DButil;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MemberDelServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String zhuanye = request.getParameter("zhuanye");

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = DButil.getConnction();
            String sql = "SELECT * FROM sss WHERE id = ? AND name = ? AND zhuanye = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, id);
            ps.setString(2, name);
            ps.setString(3, zhuanye);
            rs = ps.executeQuery();

            if (rs.next()) {
                sql = "DELETE FROM sss WHERE id = ? AND name = ? AND zhuanye = ?";
                ps = conn.prepareStatement(sql);
                ps.setString(1, id);
                ps.setString(2, name);
                ps.setString(3, zhuanye);
                ps.executeUpdate();

                response.sendRedirect(request.getContextPath() + "/dept/list");
            } else {
                request.setAttribute("errorMsg", "未找到成员，删除失败！");
                request.getRequestDispatcher("/error1.jsp").forward(request, response);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                DButil.close(conn, ps, rs);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}